using System;
using System.Collections.Generic;
using System.Text;

namespace AssetCapture
{
    public class FrameCalculate
    {
        public void MitsuResize(int choice, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            if (choice == 1)
                FullMitsuSize((int)imageWidth, (int)imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 2)
                ScaleWidthCentre((double)1280, (double)720, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 3)
                ScaleWidthTopJustify((double)1280, (double)720, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 4)
                ScaleHightCentre((double)1280, (double)720, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else
            {
                double ratioW = imageWidth / (double)1280;
                double ratioH = imageHight / (double)720;

                if (ratioW > ratioH)
                    ScaleWidthCentre((double)1280, (double)720, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
                else
                    ScaleHightCentre((double)1280, (double)720, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            }
        }

        public void RibbonResize(int choice, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH,out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            right_STW = 0;
            right_STH = 0;
            right_LW = 0;
            right_LH = 0;

            if (choice == 1)
                FullRibbonSize((int)imageWidth, (int)imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 2)
                ScaleWidthCentre((double)2048, (double)1080, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 3)
                ScaleWidthTopJustify((double)2048, (double)1080, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else if (choice == 4)
                ScaleHightCentre((double)2048, (double)1080, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            else
            {
                double ratioW = imageWidth / (double)2048;
                double ratioH = imageHight / (double)1080;

                if (ratioW > ratioH)
                    ScaleWidthCentre((double)2048, (double)1080, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
                else
                    ScaleHightCentre((double)2048, (double)1080, imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            }
        }

        public void FullStrech(int choice, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            if (choice == 1)
            {
                FullStrechWidthCentre(imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out right_STW, out right_STH, out right_LW, out right_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            }
            else
            {
                FullStrechWidthTopJustify(imageWidth, imageHight, out left_STW, out left_STH, out left_LW, out left_LH, out right_STW, out right_STH, out right_LW, out right_LH, out RectWStart, out RectHStart, out RectWidth, out RectHight);
            }
        }

        private void FullMitsuSize(int imageWidth, int imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            left_STW = 0;
            left_STH = 0;
            left_LW = imageWidth;
            left_LH = imageHight;
            RectHight = 720;
            RectHStart = 0;
            RectWStart = 0;
            RectWidth = 1280;
        }

        private void FullRibbonSize(int imageWidth, int imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            left_STW = 0;
            left_STH = 0;
            left_LW = imageWidth;
            left_LH = imageHight;
            RectHight = 1080;
            RectHStart = 0;
            RectWidth = 2048;
            RectWStart = 0;
        }

        private void ScaleWidthTopJustify(double ScreenWidth, double  ScreenHight, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            left_STW = 0;
            left_LW = (int)imageWidth;

            double Rect = ScreenWidth / imageWidth * imageHight;
            RectHight = (int)Rect;
            RectWidth = (int)ScreenWidth;

            if (RectHight > (int)ScreenHight)
            {
                RectHStart = 0;
                RectWStart = 0;
                RectHight = (int)ScreenHight;
                left_STH = (int)((imageHight - ScreenHight / ScreenWidth * imageWidth) / 2);
                left_LH = (int)(ScreenHight / ScreenWidth * imageWidth); 
            }
            else
            {
                left_STH = 0;
                left_LH = (int)imageHight;
                RectWStart = 0;
                RectHStart = 0;
            }
        }

        private void ScaleWidthCentre(double ScreenWidth, double  ScreenHight, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            left_STW = 0;
            left_LW = (int)imageWidth;

            double Rect = ScreenWidth / imageWidth * imageHight;
            RectHight = (int)Rect;
            RectWidth = (int)ScreenWidth;

            if (RectHight > (int)ScreenHight)
            {
                RectHStart = 0;
                RectWStart = 0;
                RectHight = (int)ScreenHight;
                left_STH = (int)((imageHight - ScreenHight / ScreenWidth * imageWidth) / 2);
                left_LH = (int)(ScreenHight / ScreenWidth * imageWidth); 
            }
            else
            {
                left_STH = 0;
                left_LH = (int)imageHight;
                RectWStart = 0;
                RectHStart = (int)((ScreenHight - RectHight) / 2);
            }
        }

        private void ScaleHightCentre(double ScreenWidth, double ScreenHight, double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            left_STH = 0;
            left_LH = (int)imageHight;

            double Rect = ScreenHight / imageHight * imageWidth;
            RectWidth = (int)Rect;
            RectHight = (int)ScreenHight;

            if (RectWidth > (int)ScreenWidth)
            {
                RectHStart = 0;
                RectWStart = 0;
                RectWidth = (int)ScreenWidth;
                left_STW = (int)((imageWidth - ScreenWidth / ScreenHight * imageHight) / 2);
                left_LW = (int)(ScreenWidth / ScreenHight * imageHight);
            }
            else
            {
                left_STW = 0;
                left_LW = (int)imageWidth;
                RectHStart = 0;
                RectWStart = (int)((ScreenWidth - RectWidth) / 2);
            }
        }

        private void FullStrechWidthCentre(double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            RectWStart = 0;
            RectWidth = 2048;

            double w1 = (double)256 / (double)479;
            double w2 = (double)223 / (double)479;
            double h1 = (double)2160 / (double)4311;
            double h2 = (double)2151 / (double)4311;
            double Rect = (double)3832 / imageWidth * imageHight;

            RectHight = Convert.ToInt32(Rect);

            left_STW = 0;
            left_STH = Convert.ToInt32(imageHight / (double)2 * h2);
            left_LW = Convert.ToInt32(w1 * imageWidth);
            left_LH = Convert.ToInt32(h1 * imageHight);

            right_STW = left_LW;
            right_STH = left_STH;
            right_LW = Convert.ToInt32(w2 * imageWidth);
            right_LH = left_LH;

            if (RectHight > 1080)
            {
                RectHight = 1080;
                RectHStart = 0;
            }
            else
            {
                left_STW = 0;
                left_STH = 0;
                left_LH = (int)imageHight;
                right_STH = 0;
                right_LH = left_LH;
                RectHStart = (int)((1080 - RectHight) / 2);
            }
        }

        private void FullStrechWidthTopJustify(double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectWStart, out int RectHStart, out int RectWidth, out int RectHight)
        {
            RectWStart = 0;
            RectWidth = 2048;

            double w1 = (double)256 / (double)479;
            double w2 = (double)223 / (double)479;
            double h1 = (double)2160 / (double)4311;
            double h2 = (double)2151 / (double)4311;
            double Rect = (double)3832 / imageWidth * imageHight;

            RectHight = Convert.ToInt32(Rect);

            left_STW = 0;
            left_STH = Convert.ToInt32(imageHight / (double)2 * h2);
            left_LW = Convert.ToInt32(w1 * imageWidth);
            left_LH = Convert.ToInt32(h1 * imageHight);

            right_STW = left_LW;
            right_STH = left_STH;
            right_LW = Convert.ToInt32(w2 * imageWidth);
            right_LH = left_LH;

            RectHStart = 0;

            if (RectHight > 1080)
            {
                RectHight = 1080;
            }
            else
            {
                left_STW = 0;
                left_STH = 0;
                left_LH = (int)imageHight;

                right_STH = 0;
                right_LH = left_LH;
            }
        }

        public void GetFrameAtt(double imageWidth, double imageHight, out int left_STW, out int left_STH, out int left_LW, out int left_LH, out int right_STW, out int right_STH, out int right_LW, out int right_LH, out int RectHight)
        {
            double w1 = (double)256 / (double)479;
            double w2 = (double)223 / (double)479;
            double h1 = (double)2160 / (double)4311;
            double h2 = (double)2151 / (double)4311;
            double Rect = (double)3832 / imageWidth * imageHight;

            RectHight = Convert.ToInt32(Rect);

            left_STW = 0;
            left_STH = Convert.ToInt32(imageHight / (double)2 * h2);
            left_LW = Convert.ToInt32(w1 * imageWidth);
            left_LH = Convert.ToInt32(h1 * imageHight);

            right_STW = left_LW;
            right_STH = left_STH;
            right_LW = Convert.ToInt32(w2 * imageWidth);
            right_LH = left_LH;

            if (RectHight > 1080)
            {
                RectHight = 1080;
            }
            else
            {
                left_STW = 0;
                left_STH = 0;
                left_LH = (int)imageHight;

                right_STH = 0;
                right_LH = left_LH;
            }
        }

        public void GetTickerFrame(double imageWidth, double imageHeight, out int RectHight)
        {
            double Rect = (double)2048 / imageWidth * imageHeight;

            RectHight = Convert.ToInt32(Rect);
        }
    }
}
